var app = angular.module('subscribeListApp', []);
app.controller('subscribeListController', function($scope, $http) {

	$scope.titleClass = [ {
		"li" : "w50 rline",
		"span" : "tabfouse"
	}, {
		"li" : "w50",
		"span" : ""
	} ];
	
	$scope.statusLabel = [ "进行中", "已完成" ];
	$scope.subscribeListCache = [[], []];
	$scope.subscribeList = [];
	$scope.progress = "";
	$scope.detailSubscribeID = "";

	$scope.titleClick = function(status) {
		$scope.subscribeList = $scope.subscribeListCache[status];
		$http({
	        method: 'POST',
	        url: '../query.do',
	        data: { "queryID": "subscribe.S001", "condition": [status] },  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		$scope.subscribeList = response.data.dataList;
	    		$scope.subscribeListCache[status] = $scope.subscribeList;
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
		if (status == 0) {
			$scope.titleClass = [ {
				"li" : "w50 rline",
				"span" : "tabfouse"
			}, {
				"li" : "w50",
				"span" : ""
			} ];
		} else if (status == 1) {
			$scope.titleClass = [ {
				"li" : "w50",
				"span" : ""
			}, {
				"li" : "w50 rline",
				"span" : "tabfouse"
			} ];
		} 

		$scope.progress = status;
	};
	
	$scope.goDetail = function(subscribeID) {
		$("input[name=detailSubscribeID]").val(subscribeID);
		$("form").submit();
	};
	
	$scope.titleClick(0);
});

//页面返回刷新
pushHistory();
setTimeout(function() {
	window.addEventListener("popstate", function(e) {
		window.location.href = 'homepage.do';
	}, false);
}, 300);
function pushHistory() {
	var state = {
		title : "title",
		url : "#"
	};
	window.history.pushState(state, "title", "#");
}
